#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include "smpeg.h"
#include "perso.h"
int menu( SDL_Surface *ecran, int *p_sorir)
{
    int i =0 ,clic=0, volume=100,video_mode=0; 
    int *p;
    p= &clic;
    int sortir =0  ,tempsPrecedent = 0, tempsActuel = 0;;
    SDL_Surface *surfaceVideo = NULL, *son = NULL, *titre = NULL, *bouton1 = NULL, *bouton2 = NULL, *bouton3 = NULL, *bouton4 = NULL, *bouton5 = NULL, *bouton6 = NULL;
    SDL_Surface *bouton1Clic = NULL, *bouton2Clic = NULL, *bouton3Clic = NULL, *bouton4Clic = NULL, *bouton5Clic = NULL, *bouton6Clic = NULL, *logo =NULL;
    int continuer = 1;
    SDL_Event event;
    static int maxW, maxH;
    maxW=1000;
    maxH=700;



    FILE* video = NULL;
    video = fopen("menu/options/video.txt", "r");
    if (video != NULL)
    {
        
        fscanf(video, "%d",&video_mode);
        if (video_mode ==1)
        {
            

            ecran = SDL_SetVideoMode(0, 0, 0, SDL_FULLSCREEN | SDL_DOUBLEBUF); //SDL_FULLSCREEN
            if (ecran == NULL)
            {
                fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
                exit(EXIT_FAILURE);
            }
            const SDL_VideoInfo *videoInfo;
 
            videoInfo=SDL_GetVideoInfo();
            maxW=videoInfo->current_w;
            maxH=videoInfo->current_h;
        }

        if (video_mode ==0)
        {
            ecran = SDL_SetVideoMode(maxW, maxH, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); //SDL_FULLSCREEN
    if (ecran == NULL)
    {
        fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

        }
    fclose(video); // On ferme le fichier qui a été ouvert

    }
    

    SDL_Rect position, titre_position, position_bouton2, position_bouton3, position_bouton4, position_bouton5, position_bouton6, position_logo;
    SDL_Rect position_bouton1Clic, position_bouton2Clic, position_bouton3Clic, position_bouton4Clic, position_bouton5Clic, position_bouton6Clic;
    position.x = (maxW-340)/2;
    position.y = maxH/3.5;
    position_bouton2.x = (maxW-340)/2;
    position_bouton2.y = position.y +35;
    position_bouton3.x = (maxW-340)/2;
    position_bouton3.y = position_bouton2.y +35;
    position_bouton4.x = (maxW-340)/2;
    position_bouton4.y = position_bouton3.y +35;
    position_bouton5.x = (maxW-340)/2;
    position_bouton5.y = position_bouton4.y +35;
    position_bouton6.x = (maxW-340)/2;
    position_bouton6.y = position_bouton5.y +35;

    position_bouton1Clic.x = ((maxW-340)/2 )- 40;
    position_bouton1Clic.y = maxH/3.5;
    position_bouton2Clic.x = ((maxW-340)/2 )- 40;
    position_bouton2Clic.y = position.y +35;
    position_bouton3Clic.x = ((maxW-340)/2 )- 40;
    position_bouton3Clic.y = position_bouton2.y +35;
    position_bouton4Clic.x = ((maxW-340)/2 )- 40;
    position_bouton4Clic.y = position_bouton3.y +35;
    position_bouton5Clic.x = ((maxW-340)/2 )- 40;
    position_bouton5Clic.y = position_bouton4.y +35;
    position_bouton6Clic.x = ((maxW-340)/2 )- 40;
    position_bouton6Clic.y = position_bouton5.y +35;

    titre_position.x = (maxW- 563)/2;
    titre_position.y=30;

    position_logo.x = (maxW-100);
    position_logo.y = 15;


    SMPEG_Info mpg_info;
    SMPEG *mpg = NULL;
   
    mpg = SMPEG_new("menu/video.mpg", &mpg_info, 1);
    
     
    
    
    if (SMPEG_error(mpg))
    {
                fprintf(stderr, "Impossible de charge la video video.mpg");
        exit(EXIT_FAILURE);
    }

    


    surfaceVideo = SDL_AllocSurface(SDL_HWSURFACE, maxW, maxH , ecran->format->BitsPerPixel, ecran->format->Rmask, ecran->format->Gmask, ecran->format->Bmask, ecran->format->Amask); //SDL_HWSURFACE
    if (surfaceVideo == NULL)
    {
                fprintf(stderr, "Impossible d'allouer la surface vidéo");
        exit(EXIT_FAILURE);
    }
    bouton1 = IMG_Load("menu/Boutons/bouton1.png");
    bouton2 = IMG_Load("menu/Boutons/bouton2.png");
    bouton3 = IMG_Load("menu/Boutons/bouton3.png");
    bouton4 = IMG_Load("menu/Boutons/bouton4.png");
    bouton5 = IMG_Load("menu/Boutons/bouton5.png");
    bouton6 = IMG_Load("menu/Boutons/bouton6.png");
    bouton1Clic = IMG_Load("menu/BoutonsClic/bouton1Clic.png");
    bouton2Clic = IMG_Load("menu/BoutonsClic/bouton2Clic.png");
    bouton3Clic = IMG_Load("menu/BoutonsClic/bouton3Clic.png");
    bouton4Clic = IMG_Load("menu/BoutonsClic/bouton4Clic.png");
    bouton5Clic = IMG_Load("menu/BoutonsClic/bouton5Clic.png");
    bouton6Clic = IMG_Load("menu/BoutonsClic/bouton6Clic.png");
    titre = IMG_Load("menu/titre.png");
    logo = IMG_Load("menu/logo.png");

    
    SMPEG_setdisplay(mpg, surfaceVideo, NULL,NULL);
    SMPEG_loop( mpg, -1 );
    SMPEG_play(mpg);

 
   

   
        
        


    while (continuer)
    { 
        tempsActuel = SDL_GetTicks();
        if (tempsActuel - tempsPrecedent > 30)
        {
        SDL_BlitSurface(surfaceVideo, NULL, ecran, NULL);
        SDL_BlitSurface(titre, NULL, ecran, &titre_position);
        SDL_BlitSurface(logo, NULL, ecran, &position_logo);
        SMPEG_setvolume( mpg, volume  );
        
        
        

        while (SDL_PollEvent(&event))
        {

            switch (event.type)
            {
                
                case SDL_QUIT:
                    continuer = 0;
                    sortir =1;
                    return sortir;
                    break;
                case SDL_MOUSEMOTION:
                    
                    if (event.motion.x>=position.x && event.motion.x <= position.x +330 && event.motion.y >= position.y && event.motion.y <= position.y+35 && clic==0)
                    {
                        i=1;
                        
                    }
                    else if (event.motion.x>=position_bouton2.x && event.motion.x <= position_bouton2.x +330 && event.motion.y >= position_bouton2.y && event.motion.y <= position_bouton2.y+35 && clic==0)
                    {
                        i=2;
                    }
                    else if (event.motion.x>=position_bouton3.x && event.motion.x <= position_bouton3.x +330 && event.motion.y >= position_bouton3.y && event.motion.y <= position_bouton3.y+35 && clic==0)
                    {
                        i=3;
                    }
                    else if (event.motion.x>=position_bouton4.x && event.motion.x <= position_bouton4.x +330 && event.motion.y >= position_bouton4.y && event.motion.y <= position_bouton4.y+35 && clic==0)
                    {
                        i=4;
                    }
                    else if (event.motion.x>=position_bouton5.x && event.motion.x <= position_bouton5.x +330 && event.motion.y >= position_bouton5.y && event.motion.y <= position_bouton5.y+35 && clic==0)
                    {
                        i=5;
                    }
                    else if (event.motion.x>=position_bouton6.x && event.motion.x <= position_bouton6.x +330 && event.motion.y >= position_bouton6.y && event.motion.y <= position_bouton6.y+35 && clic==0)
                    {
                        i=6;

                    }
                    else
                    {
                        i=0;
                    }
                    

                    break;

            
                case SDL_MOUSEBUTTONUP:
                {
                    if (i==1)
                    {
                        clic=1;
                    }
                    if ( i== 4)
                    {
                        clic=4;
                    }
                    if (i==6)
                    {   
                        sortir =1;
                        return sortir;
                        continuer = 0;

                    }
                }
                break;

            }
        } 
        if (SMPEG_status(mpg) != SMPEG_PLAYING)
        continuer = 0;
        
        
        if (i==0&& clic==0)
        {
            SDL_BlitSurface(bouton1, NULL, ecran, &position);
            SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
            SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
            SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
            SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
            SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
            SDL_Flip(ecran);
        }
        if (i==1&& clic==0)
            {
                SDL_BlitSurface(bouton1Clic, NULL, ecran, &position_bouton1Clic);
                SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
                SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
                SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
                SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
                SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
                SDL_Flip(ecran);
                
                
            }
        if (i==2&& clic==0)
            {
                SDL_BlitSurface(bouton1, NULL, ecran, &position);
                SDL_BlitSurface(bouton2Clic, NULL, ecran, &position_bouton2Clic);
                SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
                SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
                SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
                SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
                SDL_Flip(ecran);
            }
        if (i==3&& clic==0)
            {
                SDL_BlitSurface(bouton1, NULL, ecran, &position);
                SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
                SDL_BlitSurface(bouton3Clic, NULL, ecran, &position_bouton3Clic);
                SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
                SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
                SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
                SDL_Flip(ecran);
            }
        if (i==4&& clic==0)
            {
                SDL_BlitSurface(bouton1, NULL, ecran, &position);
                SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
                SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
                SDL_BlitSurface(bouton4Clic, NULL, ecran, &position_bouton4Clic);
                SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
                SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
                SDL_Flip(ecran);
            }
        if (i==5&& clic==0)
            {
                SDL_BlitSurface(bouton1, NULL, ecran, &position);
                SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
                SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
                SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
                SDL_BlitSurface(bouton5Clic, NULL, ecran, &position_bouton5Clic);
                SDL_BlitSurface(bouton6, NULL, ecran, &position_bouton6);
                SDL_Flip(ecran);
            }
            if (i==6&& clic==0)
            {
                SDL_BlitSurface(bouton1, NULL, ecran, &position);
                SDL_BlitSurface(bouton2, NULL, ecran, &position_bouton2);
                SDL_BlitSurface(bouton3, NULL, ecran, &position_bouton3);
                SDL_BlitSurface(bouton4, NULL, ecran, &position_bouton4);
                SDL_BlitSurface(bouton5, NULL, ecran, &position_bouton5);
                SDL_BlitSurface(bouton6Clic, NULL, ecran, &position_bouton6Clic);
                SDL_Flip(ecran);
            }
            if (clic==1)
            {

                choisir_perso(ecran,event,&clic,&maxW,&maxH,&continuer);
                
                SDL_Flip(ecran);
            }
            if (clic ==4)
            {
                sortir = option(ecran,event,&clic,&volume,&maxW,&maxH, mpg,surfaceVideo);
                *p_sorir = 1;
                
                SDL_Flip(ecran);
            }

            tempsPrecedent = tempsActuel;
        }
        else /* Si ça fait moins de 30 ms depuis le dernier tour de boucle, on endort le programme le temps qu'il faut */
        {
             SDL_Delay(30 - (tempsActuel - tempsPrecedent));
        }       
       
    }
   

    SMPEG_stop(mpg);
    SMPEG_delete(mpg);
    mpg = NULL;
  
    SDL_FreeSurface(surfaceVideo);
    SDL_FreeSurface(bouton1);
    SDL_FreeSurface(bouton2);
    SDL_FreeSurface(bouton3);
    SDL_FreeSurface(bouton4);
    SDL_FreeSurface(bouton5);
    SDL_FreeSurface(bouton6);
    SDL_FreeSurface(bouton1Clic);
    SDL_FreeSurface(bouton2Clic);
    SDL_FreeSurface(bouton3Clic);
    SDL_FreeSurface(bouton4Clic);
    SDL_FreeSurface(bouton5Clic);
    SDL_FreeSurface(bouton6Clic);
    SDL_FreeSurface(logo);
    SDL_FreeSurface(titre);
    surfaceVideo = NULL;

   



    SDL_Quit();

    
}
